<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\api\service;


use think\Db;

class IndexService
{
	/**根据条件更新字段
	 * @param $where
	 * @return array|false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	function setuserparm($userid,$parm,$info)
	{	
		return Db::name('user')->where('id', $userid)->update([$parm => $info]);
	}
    /**根据条件获取轮播
     * @param $where
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function get_slider_list($where)
    {

        return Db::name('slide_item')->where($where)->order('list_order desc')->select();

    }
	/**根据条件更新字段
	 * @param $where
	 */
	public static function up_user_param($userid,$parm,$info)
	{
		$user_   = Db::name('user')->where('id', $userid)->setInc($parm, $info);
		$system_ = Db::name('system_set')->where('id', 1)->find();
		$result  = Db::name('user')->where('id', $userid)->field('cancle_num,first_pay')->find();
		if($result['first_pay'] == 0 && $result['cancle_num'] >= $system_['cancle_num']){
			Db::name('user')->where('id', $userid)->update(['first_pay' => 1]);
		}
		if($user_){
			return true;
		}
		return false;
	}
	/**根据条件获取资费策略
	 * @param $adcode,$info
	 * @return array|false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public static function gettactics($adcode,$info,$classd,$lon,$lat)
	{
		switch ($info) {
		    case 0:
				$where['county_id'] = $adcode;
				$where['type'] = 0;
		        $result = Db::name('pricing_tactics')->where($where)->find();
				if($result && $result['tactics_array']){
					// return json_decode($result['tactics_array']);
					$where_driver['user_status'] = 2;//用户状态正常
					$where_driver['work_status'] = 1;//听单中
					$where_driver['driver_type'] = $classd;
					
					$squares = returnSquarePoint($lon, $lat);
					$ret_data = array();
					$count = 0;
					
					$result = Db::name("driver_user")
									->where($where_driver)
									->where([['car_lat', '>', $squares['right-bottom']['lat']],['car_lat', '<', $squares['left-top']['lat']],['car_lon', '>', $squares['left-top']['lng']],['car_lon', '<', $squares['right-bottom']['lng']]])
									->select();
									
					$ret_data['data']  = $result;
					$ret_data['count'] = count($result);
					return $ret_data['count'];
				}else{
					return false;
				}
		        break;
		    case 1:
			case 3:
				$where['county_id'] = $adcode;
				$result = Db::name('pricing_tactics')->where($where)->find();
				if($result && $result['tactics_array_t']){
					// return json_decode($result['tactics_array_t']);
					//查询当前司机数量
					$where_driver['driver_type'] = 5;
					$where_driver['user_status'] = 2;//用户状态正常
					$where_driver['work_status'] = 1;//听单中
					
					$squares = returnSquarePoint($lon, $lat);
					// return $squares;
					
					$ret_data = array();
					$count = 0;
					
					$result = Db::name("driver_user")
									->where($where_driver)
									->where([['car_lat', '>', $squares['right-bottom']['lat']],['car_lat', '<', $squares['left-top']['lat']],['car_lon', '>', $squares['left-top']['lng']],['car_lon', '<', $squares['right-bottom']['lng']]])
									->select();
									
					$ret_data['data']  = $result;
					$ret_data['count'] = count($result);
					
					return $ret_data['count'];
					
				}else{
					return false;
				}
            break;
            case 2:
                $where['county_id'] = $adcode;
                $result = Db::name('pricing_tactics')->where($where)->find();
                if($result && $result['tactics_array_d']){
                	// return json_decode($result['tactics_array_t']);
                	//查询当前司机数量
                	$where_driver['driver_type'] = 4;
                	$where_driver['user_status'] = 2;//用户状态正常
                	$where_driver['work_status'] = 1;//听单中
                	
                	$squares = returnSquarePoint($lon, $lat);
                	// return $squares;
                	
                	$ret_data = array();
                	$count = 0;
                	
                	$result = Db::name("driver_user")
                					->where($where_driver)
                					->where([['car_lat', '>', $squares['right-bottom']['lat']],['car_lat', '<', $squares['left-top']['lat']],['car_lon', '>', $squares['left-top']['lng']],['car_lon', '<', $squares['right-bottom']['lng']]])
                					->select();
                					
                	$ret_data['data']  = $result;
                	$ret_data['count'] = count($result);
                	
                	return $ret_data['count'];
                	
                }else{
                	return false;
                }
				break;
			default:
				return false;
		}
	}
	
	/**订单提交
	 * @param $adcode,$info
	 * @return array|false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public static function suborder($userid)
	{
		$where['user_status'] = 1;
		$where['id'] = $userid;
		$user_ = Db::name('user')->where($where)->field('first_pay')->find();
		$system_ = Db::name('system_set')->where('id', 1)->field('switch')->find();
		if($system_['switch']){
			return $system_['switch'];
		}
		return $user_['first_pay'];
		
	}
	/**时间限制判断
	 * @param 
	 */
	public static function check_time_($reservation_time)
	{
		$system_      = Db::name('system_set')->where('id', 1)->field('timepoint,timepointend')->find();
		$timepoint = $system_['timepoint'];
		$timepointend = $system_['timepointend'];
		$date = substr($reservation_time, -5, 5);
		if ($date >= $timepoint || $date <= $timepointend) {
		    return  false;
		}
		return  true;
	}
	/**订单查询
	 * @param $adcode,$info
	 * @return array|false|\PDOStatement|string|\think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public static function get_order_obj($order_number,$userid,$param)
	{
		$where[$param]     = $userid;
		$where['order_number'] = $order_number;
		$order_ = Db::name('order')->where($where)->find();
		
		return $order_;
	}
	/**根据条件更新数据
	 * @param $where
	 */
	public static function update_order_obj($orderid,$parm,$info)
	{	
		$where['order_id'] = $orderid;
		return Db::name('order')->where($where)->update([$parm => $info]);
	}
	/**根据条件更新数据
	 * @param $where
	 */
	public static function update_order_obj_array($orderid,$parm)
	{	
		$where['order_id'] = $orderid;
		return Db::name('order')->where($where)->update($parm);
	}
	/*
	*根据条件退款
	*/
    public static function cancle_refund($userid,$order_obj_)
    {
		$user_   = Db::name('user')->where('id', $userid)->setInc('balance', $order_obj_['real_amount']);
		$result  = Db::name('user')->where('id', $userid)->field('balance,jpush_id')->find();
		$remark_msg = '取消订单'.$order_obj_['order_number'].'退款'.$order_obj_['real_amount'].'元';
		$info = userbalanceLog($userid, $order_obj_['pay_method'], 3, $order_obj_['real_amount'], $result['balance'], $remark_msg, $order_obj_['order_id'], '');
		
		if($info && $user_){
			return true;
		}
		return false;
	}
	/*
	*根据条件推送消息
	*/
	public static function jpush_send_user($type,$remark_msg,$userid)
	{
		$result = Db::name('user')->where('id', $userid)->value('jpush_id');
		$param = [
		    'type' => $type,
		    'registration_id' => $result,
		    'msg' => $remark_msg
		];
		$res = hook_one('jpush_send',$param);
	}
	/*
	*根据条件推送消息
	*/
	public static function jpush_send_driver($type,$remark_msg,$driver_id)
	{
		$result = Db::name('driver_user')->where('id', $driver_id)->value('jpush_id');
		$param = [
		    'type' => $type,
		    'registration_id' => $result,
		    'msg' => $remark_msg
		];
		$res = hook_one('jpush_send', $param);
	}
	/*
	*根据条件获取帮助列表
	*/
	public static function get_help_list($type)
	{
		if($type == 1){//用户端
			return $result = Db::name('category')->field('id,name')->where('path', 'like', "0-1-%")->select();
		}else{
			return $result = Db::name('category')->field('id,name')->where('path', 'like', "0-2-%")->select();
		}
	}
	/*
	*根据条件获取帮助详情
	*/
	public static function get_help_dateils($id)
	{
		return $result = Db::name('category')->field('id,content')->where('id', $id)->find();
	}
	/*
	*根据条件获取专线列表
	*/
	public static function get_line_list($index,$adcode)
	{
		switch ($index) {
		    case 0:
				
		        break;
		    case 1:
			case 3:
	        $where['adcode'] = $adcode;
	        $result = Db::name('region_line')->where($where)->select();
	        if(count($result)){
	            return $result;
	        }else{
	            return false;
	        }
	        break;
	        case 2:
	            
	            break;
		}
	}
	/*
	*根据条件获取专线列表
	*/
	public static function get_goodstype_list()
	{
		$result = Db::name('goods_tag')->select();
		return $result;
	}
	/*
	*根据条件获取生成订单价格
	*/
	public static function get_line_money($line_id,$ride)
	{
		$amounts = [];
		//获取订单专线价格
		$where['id'] = $line_id;
		$result = Db::name('region_line')->where($where)->find();
		$amounts['regionprice'] = $result['regionprice'];//专线价格
		$amounts['allregionprice'] = $result['regionprice'] * $ride;//乘客人数专线费用
		return $amounts;
	}
	public static function get_distance_money($line_id,$info,$lon,$lat)
	{
		$where['id'] = $line_id;
		$result = Db::name('region_line')->where($where)->find();
		if($info){
			$datas = [];
			$locationparam = json_decode($result['star_location'], TRUE);
			foreach ($locationparam as $k => $v) {
				$uu = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins='. $lat .','. $lon .'&destinations='. $v['lat'] .','. $v['lng'] .'&ak=bPUIKczaCFnVGlHD7cgalCeW0pKDhDq9';
				$ch = curl_init($uu);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$cexeCute = curl_exec($ch);
				curl_close($ch);
				$distance_ = json_decode($cexeCute, true);
				
				if($distance_['status'] == 0){
					array_push($datas,$distance_['result'][0]['distance']['value']);
				}
			}
			$distance = min($datas);//获取最短距离
			return number_format($distance / 1000,2);//格式化为公里保留两位小数
		}else{
			$datas = [];
			$locationparam  = json_decode($result['end_location'], TRUE);
			foreach ($locationparam as $k => $v) {
				$uu = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins='. $lat .','. $lon .'&destinations='. $v['lat'] .','. $v['lng'] .'&ak=bPUIKczaCFnVGlHD7cgalCeW0pKDhDq9';
				$ch = curl_init($uu);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$cexeCute = curl_exec($ch);
				curl_close($ch);
				$distance_ = json_decode($cexeCute, true);
				
				if($distance_['status'] == 0){
					array_push($datas,$distance_['result'][0]['distance']['value']);
				}
			}
			$distance = min($datas);//获取最短距离
			return number_format($distance / 1000,2);//格式化为公里保留两位小数
		}
	}
	public static function insert_order_data($data)
	{
		return $info = Db::name("order")->insertGetId($data);
	}
	
	/**
	 * 根据条件获取订单状态
	 */
	public static function get_order_status($orderid)
	{
		$where['order_id'] = $orderid;
		return $info = Db::name("order")
						->alias('o')
						->join('cmf_driver_user d', 'o.driver_id = d.id','left')
						->field('o.*,d.avatar,d.user_name,d.car_model,d.car_color,d.car_brand,d.car_number,d.car_lat,d.car_lon')
						->where($where)
						->find();
	}
	/**
	 * uu跑腿路径距离接口
	 */
	public static function line_uu()
	{
	    $startlng = '117.63015';
	    $startlat = '35.510845';
	    $endlng = '118.352264';
	    $endlat = '35.056808';
	    $uu = 'http://wechat.uupaotui.com/NewOrder/BaduMethod.ashx?action=1002&start=' . $startlng . ',' . $startlat . '&end=' . $endlng . ',' . $endlat . '&mode=walking';
	    $ch = curl_init($uu);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    $cexeCute = curl_exec($ch);
	    curl_close($ch);
	    return json_decode($cexeCute, true);
	}
	/**
	 * 百度地图路径距离接口
	 */
	public static function line_distance($data)
	{
	    $startlng = $data['startLon'];
	    $startlat = $data['startLat'];
	    $endlng   = $data['endLon'];
	    $endlat   = $data['endLat'];
	    $uu = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins='. $startlat .','. $startlng .'&destinations='. $endlat .','. $endlng .'&ak=bPUIKczaCFnVGlHD7cgalCeW0pKDhDq9';
	    $ch = curl_init($uu);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    $cexeCute = curl_exec($ch);
	    curl_close($ch);
	    return json_decode($cexeCute, true);
	}
	/**
	 * 根据路径距离返回价格
	 * 围栏算法，判断一个坐标，是否在围栏里面
	 */
	public static function line_contain($param)
	{
        // if(cache('address_area_arr')){
        //     $area_arr = cache('address_area_arr');
        // }else{
		// }
		$where['id'] = $param['id'];
		$result = Db::name('region_line')->where($where)->find();
		$result['star_location'] = str_replace("lng","lon",$result['star_location']);
		$result['end_location']  = str_replace("lng","lon",$result['end_location']);
		$result['star_location'] = json_decode($result['star_location']);
		$result['end_location']  = json_decode($result['end_location']);
		return $result;
	}
	/**
	 * 获取接送机计费
	 */
	public static function pickupamount_money($adcode)
	{
		$where['county_id'] = $adcode;
		$ret = Db::name('pricing_tactics')->where($where)->find();
		if ($ret['tactics_array_t']) {
			$tactics_array = json_decode($ret['tactics_array_t'], TRUE);
			return $tactics_array['pickupamount'];
		}
		return  false;
	}
	/**
	 * 根据城市id获取城市策略计费
	 */
	public static function distance_money($adcode,$distance)
	{
		$kilometers = ceil($distance);
		$where['county_id'] = $adcode;
		$ret = Db::name('pricing_tactics')->where($where)->find();
		if ($ret['tactics_array_t']) {
			$tactics_array = json_decode($ret['tactics_array_t'], TRUE);
			$mileage = $tactics_array['mileage'];
			$starting_price = $tactics_array['is_starting_price'];
			//计算起步公里数及起步价
			foreach ($starting_price as $key => $value){//起步价集合循环
				if ($kilometers <= round($value['mileageKilometre']) || $key == count($starting_price)-1) {//起步价
					$totalPrice['startkilometres'] = $value['mileageKilometre'];//起步公里数
					$totalPrice['startmoney'] = $value['mileageMoney'];//起步价
					$totalPrice['totalPrice'] = $value['mileageMoney'];
					$totalPrice['money'] =0.00;
					$totalPrice['kilometres'] = 0.00;
					break;
				}
			}
			//计算里程公里数及里程费用
			$mileage_kilometre = 0.00 ;
			$mileage_money = 0.00;
			$mileage_time = 0;
			foreach ($mileage as $key1 => $value1){//起步价集合循环
				if ($kilometers >= round($totalPrice['startkilometres'])&& $kilometers <= round($value1['kilometre'])) {//小于等于起步价，则里程费0，否则计算里程费
					$mileage_money += $mileage[$key1 - 1]['money'];
	//                dump($mileage_money);
					$mileage_kilometre += $kilometers - $mileage[$key1 - 1]['kilometre'];
	//                dump($mileage_kilometre);
					$mileage_money += $mileage_kilometre * $mileage[$key1]['pricePer'];
	//                dump($mileage_money);
					break;
				}
			}
			if($mileage_kilometre > 0 ){
				$totalPrice['totalPrice'] =$mileage_money;
				$totalPrice['money'] =$mileage_money-$totalPrice['startmoney'];
				$totalPrice['mileage_time'] =$mileage_time;
			}
		}
		return $totalPrice;
	}
	/**
	 * 根据城市id获取城市策略计费    专车计费
	 */
	public static function distance_train_money($adcode,$distance)
	{
		$kilometers = ceil($distance);
		$where['county_id'] = $adcode;
		$ret = Db::name('pricing_tactics')->where($where)->find();
		if ($ret['tactics_array']) {
			$tactics_array = json_decode($ret['tactics_array'], TRUE);
			$mileage = $tactics_array['mileage'];
			$starting_price = $tactics_array['is_starting_price'];
			//计算起步公里数及起步价
			foreach ($starting_price as $key => $value){//起步价集合循环
				if ($kilometers <= round($value['mileageKilometre']) || $key == count($starting_price)-1) {//起步价
					$totalPrice['startkilometres'] = $value['mileageKilometre'];//起步公里数
					$totalPrice['startmoney'] = $value['mileageMoney'];//起步价
					$totalPrice['totalPrice'] = $value['mileageMoney'];
					$totalPrice['money'] =0.00;
					$totalPrice['kilometres'] = 0.00;
					break;
				}
			}
			//计算里程公里数及里程费用
			$mileage_kilometre = 0.00 ;
			$mileage_money = 0.00;
			$mileage_time = 0;
			foreach ($mileage as $key1 => $value1){//起步价集合循环
				if ($kilometers >= round($totalPrice['startkilometres'])&& $kilometers <= round($value1['kilometre'])) {//小于等于起步价，则里程费0，否则计算里程费

					$mileage_money += $mileage[$key1 - 1]['money'];
	//                dump($mileage_money);
					$mileage_kilometre += $kilometers - $mileage[$key1 - 1]['kilometre'];
	//                dump($mileage_kilometre);
					$mileage_money += $mileage_kilometre * $mileage[$key1]['pricePer'];
	//                dump($mileage_money);
					break;
				}
			}
			if($mileage_kilometre > 0 ){
				$totalPrice['totalPrice'] =$mileage_money;
				$totalPrice['money'] =$mileage_money-$totalPrice['startmoney'];
				$totalPrice['mileage_time'] =$mileage_time;
			}
		}
		return $totalPrice;
	}
	/**
	 * 根据城市id获取城市策略计费    货车计费
	 */
	public static function distance_train_money3($adcode,$distance)
	{
		$kilometers = ceil($distance);
		$where['county_id'] = $adcode;
		$ret = Db::name('pricing_tactics')->where($where)->find();
		if ($ret['tactics_array_h']) {
			$tactics_array = json_decode($ret['tactics_array_h'], TRUE);
			$mileage = $tactics_array['mileage'];
			$starting_price = $tactics_array['is_starting_price'];
			//计算起步公里数及起步价
			foreach ($starting_price as $key => $value){//起步价集合循环
				if ($kilometers <= round($value['mileageKilometre']) || $key == count($starting_price)-1) {//起步价
					$totalPrice['startkilometres'] = $value['mileageKilometre'];//起步公里数
					$totalPrice['startmoney'] = $value['mileageMoney'];//起步价
					$totalPrice['totalPrice'] = $value['mileageMoney'];
					$totalPrice['money'] =0.00;
					$totalPrice['kilometres'] = 0.00;
					break;
				}
			}
			//计算里程公里数及里程费用
			$mileage_kilometre = 0.00 ;
			$mileage_money = 0.00;
			$mileage_time = 0;
			foreach ($mileage as $key1 => $value1){//起步价集合循环
				if ($kilometers >= round($totalPrice['startkilometres'])&& $kilometers <= round($value1['kilometre'])) {//小于等于起步价，则里程费0，否则计算里程费
					$mileage_money += $mileage[$key1 - 1]['money'];
	//                dump($mileage_money);
					$mileage_kilometre += $kilometers - $mileage[$key1 - 1]['kilometre'];
	//                dump($mileage_kilometre);
					$mileage_money += $mileage_kilometre * $mileage[$key1]['pricePer'];
	//                dump($mileage_money);
					break;
				}
			}
			if($mileage_kilometre > 0 ){
				$totalPrice['totalPrice'] =$mileage_money;
				$totalPrice['money'] =$mileage_money-$totalPrice['startmoney'];
				$totalPrice['mileage_time'] =$mileage_time;
			}
			return $totalPrice;
		}
		return false;
	}
	/**
	 * 根据城市id获取城市策略计费    货车计费
	 */
	public static function distance_train_money4($adcode,$distance)
	{
		$kilometers = ceil($distance);
		$where['county_id'] = $adcode;
		$ret = Db::name('pricing_tactics')->where($where)->find();
		if ($ret['tactics_array_d']) {
			$tactics_array = json_decode($ret['tactics_array_d'], TRUE);
			$mileage = $tactics_array['mileage'];
			$starting_price = $tactics_array['is_starting_price'];
			//计算起步公里数及起步价
			foreach ($starting_price as $key => $value){//起步价集合循环
				if ($kilometers <= round($value['mileageKilometre']) || $key == count($starting_price)-1) {//起步价
					$totalPrice['startkilometres'] = $value['mileageKilometre'];//起步公里数
					$totalPrice['startmoney'] = $value['mileageMoney'];//起步价
					$totalPrice['totalPrice'] = $value['mileageMoney'];
					$totalPrice['money'] =0.00;
					$totalPrice['kilometres'] = 0.00;
					break;
				}
			}
			//计算里程公里数及里程费用
			$mileage_kilometre = 0.00 ;
			$mileage_money = 0.00;
			$mileage_time = 0;
			foreach ($mileage as $key1 => $value1){//起步价集合循环
				if ($kilometers >= round($totalPrice['startkilometres'])&& $kilometers <= round($value1['kilometre'])) {//小于等于起步价，则里程费0，否则计算里程费
					$mileage_money += $mileage[$key1 - 1]['money'];
	//                dump($mileage_money);
					$mileage_kilometre += $kilometers - $mileage[$key1 - 1]['kilometre'];
	//                dump($mileage_kilometre);
					$mileage_money += $mileage_kilometre * $mileage[$key1]['pricePer'];
	//                dump($mileage_money);
					break;
				}
			}
			if($mileage_kilometre > 0 ){
				$totalPrice['totalPrice'] =$mileage_money;
				$totalPrice['money'] =$mileage_money-$totalPrice['startmoney'];
				$totalPrice['mileage_time'] =$mileage_time;
			}
			return $totalPrice;
		}
		return false;
	}
	/**
	 * 根据城市id获取城市策略加价计费
	 */
	public static function line_train_order_price($adcode,$classd)
	{
		if($classd == 1){//专车计费
			$datas = [];
			$datas['addprice'] = 0;
			$datas['addtimes'] = '';
			$where['county_id'] = $adcode;
			$ret = Db::name('pricing_tactics')->where($where)->find();
			if ($ret['tactics_array']) {
				$tactics_array = json_decode($ret['tactics_array'], TRUE);
				$temporary = $tactics_array['temporary'];
				if($temporary['addtimes_reson']){
					$datas['addprice'] = $temporary['addtimes_reson'];//临时加价
					$datas['addtimes'] = $temporary['addtimes'];//临时加价原因
				}
			}
			return $datas;
		}else if($classd == 2){//出租车计费
			$datas = [];
			$datas['addprice'] = 0;
			$datas['addtimes'] = '';
			$where['county_id'] = $adcode;
			$ret = Db::name('pricing_tactics')->where($where)->find();
			if ($ret['tactics_array']) {
				$tactics_array = json_decode($ret['tactics_array'], TRUE);
				$temporary = $tactics_array['temporary'];
				if($temporary['addtimes_reson']){
					$datas['addprice'] = $temporary['addtimes_reson'];//临时加价
					$datas['addtimes'] = $temporary['addtimes'];//临时加价原因
				}
			}
			return $datas;
		}else if($classd == 3){//货车计费
			$datas = [];
			$datas['addprice'] = 0;
			$datas['addtimes'] = '';
			$where['county_id'] = $adcode;
			$ret = Db::name('pricing_tactics')->where($where)->find();
			if ($ret['tactics_array_h']) {
				$tactics_array = json_decode($ret['tactics_array_h'], TRUE);
				$temporary = $tactics_array['temporary'];
				if($temporary['addtimes_reson']){
					$datas['addprice'] = $temporary['addtimes_reson'];//临时加价
					$datas['addtimes'] = $temporary['addtimes'];//临时加价原因
				}
			}
			return $datas;
		}else{//代驾计费
			$datas = [];
			$datas['addprice'] = 0;
			$datas['addtimes'] = '';
			$where['county_id'] = $adcode;
			$ret = Db::name('pricing_tactics')->where($where)->find();
			if ($ret['tactics_array_d']) {
				$tactics_array = json_decode($ret['tactics_array_d'], TRUE);
				$temporary = $tactics_array['temporary'];
				if($temporary['addtimes_reson']){
					$datas['addprice'] = $temporary['addtimes_reson'];//临时加价
					$datas['addtimes'] = $temporary['addtimes'];//临时加价原因
				}
			}
			return $datas;
		}
	}
	/**
	 * 根据城市id获取城市策略加价计费
	 */
	public static function line_markup_price($adcode,$classd,$ride)
	{
		switch ($classd) {
		    case 0:
				
		        break;
		    case 10:
				$datas = [];
				$datas['addprice'] = 0;
				$datas['addtimes'] = '';
				$where['county_id'] = $adcode;
				$ret = Db::name('pricing_tactics')->where($where)->find();
				if ($ret['tactics_array_t']) {
					$tactics_array = json_decode($ret['tactics_array_t'], TRUE);
					$temporary = $tactics_array['temporary'];
					if($temporary['addtimes_reson']){
						$datas['addprice'] = $temporary['addtimes_reson'];//临时加价
						$datas['addtimes'] = $temporary['addtimes'];//临时加价原因
					}
				}
				return $datas;
				break;
		    case 11:
		        $datas = [];
		        $datas['addprice'] = 0;
		        $datas['addtimes'] = '';
		        $datas['discount'] = '';
		        $where['county_id'] = $adcode;
		        $ret = Db::name('pricing_tactics')->where($where)->find();
		        if ($ret['tactics_array_t']) {
		        	$tactics_array = json_decode($ret['tactics_array_t'], TRUE);
		        	$temporary = $tactics_array['temporary'];
		        	if($temporary['addtimes_reson']){
		        		$datas['addprice'] = $temporary['addtimes_reson'];//临时加价
		        		$datas['addtimes'] = $temporary['addtimes'];//临时加价原因
		        	}
					$chartered_price = $tactics_array['chartered_price'];
					foreach ($chartered_price as $key => $value){
						if ($ride <= $value['charterednum']) {
							$datas['discount'] = $value['charteredmoney'];
							break;
						}
					}
		        }
		        return $datas;
		        break;
			case 12:
			case 13:
				$datas = [];
				$datas['addprice'] = 0;
				$datas['addtimes'] = '';
				$datas['discount'] = '';
				$where['county_id'] = $adcode;
				$ret = Db::name('pricing_tactics')->where($where)->find();
				if ($ret['tactics_array_t']) {
					$tactics_array = json_decode($ret['tactics_array_t'], TRUE);
					$temporary = $tactics_array['temporary'];
					if($temporary['addtimes_reson']){
						$datas['addprice'] = $temporary['addtimes_reson'];//临时加价
						$datas['addtimes'] = $temporary['addtimes'];//临时加价原因
					}
					$datas['discount'] = $tactics_array['pickupamount'];
				}
				return $datas;
				break;
		}
		
	}
	/**
	 * 根据ID更新用户registrationID
	 */
	public static function save_registration($userid,$registrationId)
	{
		return Db::name("user")->where(array('id' => $userid))->update(['jpush_id' => $registrationId]);
	}
	
	/**
	 * 根据ID更新用户registrationID
	 */
	public static function evaluate_user($userid,$order_id,$text,$value)
	{
		$result_  = Db::name('order')->where(array('order_id' => $order_id,'evaluate_time' => 0))->find();
		if(!$result_){
			return  false ;
		}
		$where['order_id'] = $order_id;
		$ret_data = Db::name('order')->where($where)->update(array('evaluate' => $value,'evaluate_text' => $text,'evaluate_time' => time()));
		if(!$ret_data){
			return  false ;
		}
		
		$driver_pating       = Db::name('driver_user')->where('id', $result_['driver_id'])->setInc('user_pating', $value);
		$driver_orders_num   = Db::name('driver_user')->where('id', $result_['driver_id'])->setInc('orders_num', 1);
		if(!$driver_pating || !$driver_orders_num){
			return  false ;
		}
		$result = Db::name('driver_user')->where('id', $result_['driver_id'])->field('jpush_id')->find();
		$remark_msg = '已评价,单号(' . $result_['order_number'] . ')用户已评价';
		$param = [
		    'type' => 2,
		    'registration_id' => $result['jpush_id'],
		    'msg' => $remark_msg
		];
		$res = hook_one('jpush_send', $param);
		
		return true;
	}
	/**
	 * 根据ID更新用户registrationID
	 */
	public static function get_user_messages($userid,$utype)
	{
		$msg_sys_obj  = Db::name('message_sys')->where(array('uid' => $userid, 'message_type' => $utype))->find();
		if ($msg_sys_obj) {//用户系统消息修改
			$where = 1;
			if ($msg_sys_obj['login_update_time'])
				$datatime = strtotime($msg_sys_obj['login_update_time']);
				$where .= " and time >= '" . $datatime . "'";
			$where .= " and message_type = " . $utype;
			
			$msg_objs = Db::name("message")
			    ->field("count(id) as counts, group_concat(id) as msg_ids")
				->where($where)
			    ->find();
			if ($msg_objs && $msg_objs['msg_ids']) {
				if ($msg_sys_obj['unread_msgids']) {
					$data['unread_msgids'] = $msg_sys_obj['unread_msgids'] . ',' . $msg_objs['msg_ids'];
					$data['unread_counts'] = $msg_sys_obj['unread_counts'] + $msg_objs['counts'];
				} else {
					$data['unread_msgids'] = $msg_objs['msg_ids'];
					$data['unread_counts'] = $msg_objs['counts'];
				}
				$data['login_update_time'] = date('Y-m-d H:i;s', time());
				
				$up_ret = Db::name("message_sys")->where(array('sysmsg_id' => $msg_sys_obj['sysmsg_id']))->update($data);
				if ($up_ret) {
					return true;
				} else{
					return false;
				}
			}else{
				return false;
			}
		}else{
			$msg_objs = Db::name("message")
                ->field("count(id) as counts, group_concat(id) as msg_ids")
                ->find();
			if ($msg_objs) {
				$data['unread_msgids'] = $msg_objs['msg_ids'];
				$data['unread_counts'] = $msg_objs['counts'];
				$data['read_counts'] = 0;
				$data['login_update_time'] = date('Y-m-d H:i;s', time());
				$data['uid'] = $userid;
				$data['message_type'] = $utype;
				$info = Db::name("message_sys")->insertGetId($data);
				if ($info) {
					return true;
				} else{
					return false;
				}
			}
		}
	}
}
